/**
* @ProjectName: ShopSytem
* @Title: AdminFilter.java 
* @Package com.element5th.shop.servlet 
* @Description: 等后天的请求进行拦截，将不合法的请求转到登录页
* @author 第五元素小组   
* @date 2012-10-26 16:05:04 
* @version V1.0   
*/
package com.element5th.shop.servlet;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Servlet Filter implementation class AdminFilter
 */
@WebFilter(urlPatterns="/page/admin/page/*")
public class AdminFilter implements Filter {

    /**
     * Default constructor. 
     */
    public AdminFilter() {
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
	}

	/**
	 * 此方法中进行相关的拦截操作，如验证用户是否已登录等
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
			throws IOException, ServletException {
		HttpSession sess = ((HttpServletRequest)request).getSession();
		if(sess.getAttribute("loginDto") != null)
			chain.doFilter(request, response);
		else {
			String msg = "请先登录";
			String path = "/page/admin/login.jsp";
			request.setAttribute("msg", msg);
			request.getServletContext().getRequestDispatcher(path).forward(request, response);
		}
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
	}

}
